# import pymysql
# import time
# import random
#
#
# def make_userid():
#     uid = int(time.time())
#     before = random.randint(1, 50)
#     after = random.randint(50, 99)
#     uid = f"{before}{uid}{after}"
#     return uid
#
#
# def check_pwd(pwd):
#     if len(pwd) <= 8:
#         return False
#     else:
#         a = b = c = d = 0
#         for i in msg:
#             if i.isdigit():
#                 a = 1
#             elif i.isupper():
#                 b = 1
#             elif i.islower():
#                 c = 1
#             else:
#                 d = 1
#         if a + b + c + d < 3:
#             return False
#         else:
#             return True
#
#
# def mydb(sql):
#     db = pymysql.connect(user='test', password='test123',
#                          host='192.168.0.125', port=3306,
#                          database='userinfo', charset='utf8mb4')
#     cs = db.cursor()
#     text = "select * from myxitong;"
#     cs.execute(text)
#     data = cs.fetchall()
#     db.commit()
#     db.close()
#     return data
#
#
# # import json
# #
# # f = open('data.json', 'r', encoding='utf-8')
# # data = json.load(f)
# # f.close()
#
# msg = """欢迎来到xxx 用户系统，请输入您的操作指令
# 0 >>> 退出
# 1 >>> 注册
# 2 >>> 登录
# 3 >>> 修改密码
# 4 >>> 删除用户"""
# print(msg)
#
# import json
#
# f = open('user.json', 'r', encoding='utf-8')
# user = json.load(f)
# f.close()
#
# while True:
#     test = input('请输入您的指令：')
#
#     if test == '0':
#         break
#     elif test == '1':
#         # 用户输入用户名
#         username = input('请输入用户名：')
#         if 1 <= len(username) <= 20:
#             data = mydb(f"select * from user where uname = '{username}';")
#             if len(data) == 0:
#                 # 没有该用户
#                 password = input('请输入密码：')
#                 if check_pwd(pwd=password):
#                     # 合法 就写入数据库
#                     uid = make_userid()
#                     mydb(f"insert into user values({uid},'{username}','{password}')")
#                 else:
#                     print('密码不合法。。。')
#             else:
#                 print('该用户已存在')
#         else:
#             print('用户名不合法。。。。')
#
#     elif test == '2':
#         username1 = input('输入你的用户名')
#         data = mydb(f"select uname from user where uname = '{username1}';")
#         if len(data) == 0:
#             print('没有账号请注册')
#
#         else:
#             password1 = input('请输入密码')
#             data = mydb(f"select upassword from user where uname = '{password1}';")
#             data = str(data[0][0])
#             if data == password1:
#                 print('登陆成功')
#             else:
#                 print('用户名或密码错误')
#     elif test == '3':
#         password = input('请输入你的密码')
#         new_password = input('请输入你的新密码')
#         if password != mydb(f"select upassword from user where uname = '{password}';"):
#             print('密码输入错误或者两次密码输入一样')
#         else:
#             print('修改成功')
#     elif test == '4':
#         username = input('请输入你的用户名')
#         password = input('请输入你的密码')
#         if username == mydb(f"select * from user where uname = '{username}';") and mydb(f"select * from user where "
#                                                                                         f"uname = '{password}';"):
#             text = "delete from mydb where = {uid};"
#
#             print('删除成功')
#         else:
#             print('用户名或密码错误')
#     else:
#         print('指令输入有误')
#
# f = open("data.json", 'w', encoding='UTF-8')
# json.dump(data, f, ensure_ascii=False)
# f.close()


import time
import pymysql


import json


# f = open("data.json", 'r', encoding='UTF-8')
# data = json.load(f)

def mydb(sql):
    # 建立数据连接
    db = pymysql.connect(
        user='test',  # 用户名
        password='test123',  # 密码
        host='192.168.0.125',  # 数据库ip
        port=3306,  # 数据库端口
        database="userinfo",  # 数据库名称
        charset='utf8mb4')  # 字符编码
    # 建立游标 (游标的作用：执行SQL语句，获取查询结果)
    cs = db.cursor()
    # 2、通过 游标 执行SQL
    cs.execute(sql)
    # 3、获取查询的结果，如果执行的不是 查询语句，返回（）
    data = cs.fetchall()  # 以元组方式返回查询结果 ， ((第一条数据),(第二条数据)  )
    # 对数据进行了 增删改 需要 commit 提交 或者 rollback 撤销、回滚
    db.commit()
    # 关闭连接
    db.close()
    return data


def userid():
    id = int(time.time() * 1000000)
    return id


def check_password(pwd):
    if len(password) <= 8:
        return False
    else:
        a = b = c = d = 0
        for i in password:
            if i.isupper():
                a = 1
            elif i.islower():
                b = 1
            elif i.isdigit():
                c = 1
            else:
                d = 1
        if a + b + c + d >= 3:
            return True
        else:
            return False


print('请输入你的操作指令：0》》》退出')
print('请输入你的操作指令：1》》》注册')
print('请输入你的操作指令：2》》》登录')
print('请输入你的操作指令：3》》》修改密码')
print('请输入你的操作指令：4》》》删除用户')
# f = open("data.json", 'r', encoding='UTF-8')
# data = json.load(f)
while True:
    dadada = input('你的指令为：')
    if dadada == "0":
        break
    # 注册
    elif dadada == '1':
        username = input('请输入注册用户名：')
        if 0 <= len(username) <= 20:
            data = mydb(f"select * from user where uname='{username}'")
            if len(data) == 0:
                password = input('请输入密码：')
                if check_password(pwd=password):
                    id = userid()
                    mydb(f"insert into user values({id},'{username}','{password}')")
                    print('注册成功')
                else:
                    print('密码不合法')
            else:
                print('该用户已存在')
        else:
            print('用户名不合法')
    # 登录
    elif dadada == '2':
        username = input('请输入用户名：')
        data = mydb(f"select uname from user where uname='{username}'")
        if len(data) == 0:
            print('该用户不存在，请先注册')
        else:
            password = input('请输入密码：')
            data = mydb(f"select upassword from user where uname= '{username}';")
            data = str(data[0][0])
            if data == password:
                print('登陆成功')
            else:
                print('密码错误')
    # 修改密码
    elif dadada == '3':
        username = input('请输入你的用户名：')
        data = mydb(f"select uname from user where uname='{username}';")
        if len(data) == 0:
            print('该用户尚未注册')

        else:
            time.sleep(10)
            password = input('请输入你的旧的密码：')
            data = mydb(f"select upassword from user where uname ='{username}';")
            data = str(data[0][0])
            if data == password:
                s = input('请输入新密码：')
                if check_password(pwd=s):
                    mydb(f"update user set upassword ='{s}' where uname = '{username}';")
                    print('修改成功')
                elif s == password:
                    print('新密码不能和旧密码一样')
                else:
                    print('密码不合法且两次密码不能一致')
            else:
                print('原来的密码输入错误')
    elif dadada == '4':
        username = input('请输入你的用户名：')
        data = mydb(f"select uname from user where uname = '{username}'")
        if len(data[0][0]) == 0:
            print('该用户没注册过，无法删除')
        else:
            password = input('请输入你的密码：')
            data = mydb(f"select upassword from user where uname= '{username}'")
            data = str(data[0][0])
            if data == password:
                mydb(f"delete from user where uname= '{username}'")
                print('删除成功')
            else:
                print('密码错误')
    else:
        print('指令错误')





